<?php

require_once('jhtraq.php');
class JhtraqModelUsergroups extends JhtraqModelJhtraq
{
	
	function getPaged()
	{
		$mainframe =& JFactory::getApplication();
		$limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		jimport('joomla.html.pagination');
		//$search = JRequest::getVar('search');
		
		$query = "Select SQL_CALC_FOUND_ROWS * from #__jhtraq_usergroups";
		
		
		
		$query .= ' ORDER BY id ASC';
		
		$this->db->setQuery($query,$limitstart, $limit );
		$r =&$this->db->loadAssocList();
		//print_r($this->db);
		
		$this->db->setQuery('SELECT FOUND_ROWS();');
		$this->_pagination = new JPagination( $this->db->loadResult(), $limitstart, $limit );
		
		return $r;
	}
	
	function get($id = 0)
	{
		$this->db->setQuery("Select * from #__jhtraq_usergroups where id='".(int)$id."'");
		$group = $this->db->loadObject();
		return $group;
	}
	
	function save($group)
	{
		if((int)$group->id == 0)
		{
			$this->db->setQuery("Insert into #__jhtraq_usergroups set name='".$this->db->getEscaped($group->name)."'");
		}else{
			$this->db->setQuery("Update #__jhtraq_usergroups set name='".$this->db->getEscaped($group->name)."' where id='".$group->id."'");
		}
		$this->db->query();
	}
	
	function remove($group)
	{
		$this->db->setQuery("Delete from #__jhtraq_usergroups where id='".$group."'");
		$this->db->query();
	}
	
	/**
	 * @name getAll
	 * @param none
	 * @return array
	 */
	function getAll()
	{
		$this->db->setQuery("Select name,id from #__jhtraq_usergroups");
		$list =  $this->db->loadAssocList();
		$return = array();
		foreach($list as $item)
		{
			$return[$item['id']] = $item['name'];
		}
		return $return;
	}
}